/**
 * Method that get blog users list from server and display in 
 * the drop-down list, clean existing blogs if selection changes
 * @method getBloggers
 */
function getBloggers() {
	$.ajax({
		url:"api/getBloggers.py",
		type:"get",
		data:"type=json",
		dataType:"json",
		success:function(data) {
			for(var i in data.Blogs) {
				var blog_user = data.Blogs[i];
				if(blog_user == "51158405") {
					$("#blog_users").append("<option value='" + blog_user + "' selected='selected'>" + blog_user + "</option>");
				}
				else {
					$("#blog_users").append("<option value='" + blog_user + "'>" + blog_user + "</option>");
				}
				blogusers.push(blog_user);
			}			
		},
		error:function(xhr, ajaxOptions, thrownError) {
			$("#blog_users").append("<option value='Error in Retrieving Blog Users'></option>");
			alert(xhr.status);
            alert(thrownError);
		}
	});
	
}

/**
 * return number of comment
 * @method countComment
 * @param {String}id id of a blog post
 */
function countComment(id) {
	$.ajax({
		url:"api/countComment.py",
		data:"id=" + id,
		success:function(data) {
			return data;
		}
	});
}

/**
 * convert the content using regular expression
 * @method convertContent
 * @param {String}content content of the blog
 */
function convertContent(content) {
	content = "<p>" + content;
	content = content.replace(/\n\n/g, "</p><p>");
	content = content.replace(/\n/g, "<br />");
	content += "</p>";
	return content;
}

/**
 * append a blog in the content division
 * @method appendBlog
 * @param {Object}blog object blogpost
 */
function appendPost(post) {

	$("#content").prepend(
			"<div class='post' id='" + post.id + "'>" +
			"<div class='post_meta'>" +
			"<p class='author'>Author<span>" + post.author + "</span></p>" +
			"<p class='date'>Date<span>" + post.date + "</span><span>" + post.time + "</span></p>" +
			"</div><div class='post_content'><h2 class='title'>" + post.title + "</h2>" +
			"<div class='content'>" + convertContent(post.content) + "</div>" +
			"<div class='post_function'><a rel='" + post.id + "' class='edit_btn' href='#edit_popup'>Edit</a>" +
			"<a rel='" + post.id + "' class='delete_btn' href='#delete_popup'>Delete</a>" +
			"<a href='api/getSinglePost.py?id=" + post.id + "'>View Comments</a></div>" +
			"<div class='cleaner'></div></div><div class='cleaner'></div>"	// post id in href of edit and delete
			);	
}


/**
 * append a blog list in the bloglist division
 * @method appendBloglist
 * @param {Object}blog object blogpost
 */
function appendBloglist(post) {

	$("#bloglist_div").prepend(
			"<li><a class='bloglist_link' href='#" + post.id + "' />"
			+ post.title + "</a></li>");
}


/**
 * Method that get all posts of a user from the server and display on the screen
 * @method getPost
 * @param {String}author The id of blog author
 */
function getPosts(author) {
	$.ajax({
		url:"api/getPosts.py",
		type:"get",
		data:"author=" + author + "&type=json",
		dataType:"json",
		beforeSend:function() {
			$("#content").html("");
			$("#bloglist_div").html("");
			$("#loading").show();
		},
		success:function(data) {
			// empty the current value inside global variable
			blogposts = [];
			
			for(var i in data.BlogList) {
				
				var blog = data.BlogList[i];
				
				// format blog date & time
				var date_time = blog.updated.split('T');
				var date = date_time[0];
				var time = date_time[1].split('.')[0];
				
				// get data into the blogpost object
				var post = new blogpost(blog.id, blog.author, blog.title, blog.content, date, time);
				
				// add to blogposts array (global variable)
				blogposts.push(post);
				
				// print in the content division
				appendPost(post); 
				// print in the bloglist(sidebar) division
				//appendBloglist(post);
			}
			
			$("#loading").hide();
			// activate edit and delete button, and bloglist links
			buttonActivate();
		},
		error:function(xhr, ajaxOptions, thrownError) {
			$("#content").html("<h3>Error in retrieve blogs</h3>");
		}
	});
}

/**
 * Method of handling post creation
 * @method createPost
*/ 
function createPost(author, password, title, content) {
	if( validateForm() == true ) {
		$.ajax({
			url:"api/createPost.py",
			type:"post",
			data:"username=" + author + "&password=" + password + "&title=" + title + "&content=" + content,
			beforeSend:function() {
				$("#create_loading").show();
			},
			success: function(data) {
				$.colorbox.close();	// close the window
				$("#create_loading").hide();
				getPosts(author);	// cannot use currentUser
			},
			error: function(xhr, ajaxOptions, thrownError) {
				$("#create_error").html("Incorrect Password.");
			}
		});
	}
	return false;
}


/**
 * Method of handling post editing
 * triggered when submit button is clicked and submit the form
 * @method editPost
 */
function editPost(author, password, title, content, id) {
	$.ajax({
		url:"api/editPost.py",
		type:"post",
		data:"author=" + author + "&password=" + password + "&id=" + id + "&title=" + title + "&content=" + content,
		beforeSend:function() {
			$("#edit_loading").show();
		},
		success:function(data) {
			$("#edit_loading").hide();
			$("#edit_error").html("");
			$("#edit_pw").val("");
			$.colorbox.close();
			getPosts(author);
		},
		error:function(xhr, ajaxOptions, thrownError) {
			$("#edit_error").html("Incorrect Password.");
			$("#edit_loading").hide();			
		}
	});
}

/**
 * Method of handling post deletion
 * @method deletePost
 */
function deletePost(author, password, id) {
	$.ajax({
		url:"api/deletePost.py",
		type:"post",
		data:"author=" + author + "&password=" + password + "&id=" + id,
		beforeSend:function() {
			$("#delete_loading").show();			
		},
		success:function(data) {
			$("#delete_loading").hide();
			$("#delete_error").html("");
			$.colorbox.close();
			$("#"+id).hide("fast");
		},
		error:function(xhr, ajaxOptions, thrownError) {
            $("#delete_error").html("Incorrect Password.");
			$("#delete_loading").hide();
		}
	});
}

